const { name } = require("ejs")
const express = require("express")
const app = express()
const path = require("node:path")

const STUARR = [
    {
        name :"孙悟空",
        age :"18",
        gender : "男",
        address :"花果山"
    },{
        name :"猪八戒",
        age :"28",
        gender : "男",
        address :"高老庄"
    },
    {
        name :"沙和尚",
        age :"38",
        gender : "男",
        address :"流沙河"
    },
]

let na = "猪八戒"

// 将ejs设置为默认的模板引擎
app.set("view engine" ,"ejs")
// a配置模板的路径
app.set("views", path.resolve(__dirname,"views"))

app.use(express.static(path.resolve(__dirname,"./public")))
// 配置解析请求体
app.use(express.urlencoded({extended:true}))

app.get("/hello",(req,res) => {
    res.send("hello")
    
})

app.get("/students",(req,res) => {
    // <%= %>在ejs中输出内容时 他会自动对字符串中的特殊符号进行转义
    // 主要是避免xss攻击
    // <%- %>
    res.render("students",{ na })
})

app.get("/set_name",(req,res) => {
    na = req.query.name
    res.send("修改成功")
})



app.use((req,res) => {
    res.status(404)
    res.send("你的服务器已经被外星人劫持")
})



app.listen(3000, () => {
    console.log("服务器已经开启");
})